package www.ym.com.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import www.ym.com.common.CustomException;
import www.ym.com.common.R;
import www.ym.com.entity.Account;
import www.ym.com.entity.AccountLog;
import www.ym.com.entity.Address;
import www.ym.com.service.AccountLogService;
import www.ym.com.service.AccountService;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/account")
public class AccountController {

    @Autowired
    private AccountService accountService;

    @Autowired
    private AccountLogService accountLogService;


    /**
     * 新增账户
     *
     * @param account
     * @return
     */
    @PostMapping("/add")
    public R<String> add(@RequestBody Account account) {
        //新增条数不能大于5条
        List<Account> list = accountService.list();
        if (list.size() == 5) {
            return R.error("银行卡最多只能增加5条");
        }

        LambdaQueryWrapper<Account> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(account.getBankCardNumber() != "", Account::getBankNumber, account.getBankCardNumber());
        int count = accountService.count(wrapper);

        //判断银行卡是否已添加
        if (count != 1) {
            account.setTotalMenoy(0);
            account.setKyMenoy(0);
            account.setDjMenoy(0);
            //设置是否默认
            if (list.size() == 0) {
                account.setIsDefault(0);
                accountService.save(account);
            } else {
                account.setIsDefault(1);
                accountService.save(account);
            }
        } else {
            return R.error("该银行卡已添加");
        }

        return R.success("", "新增成功");
    }

    @PostMapping("/isDefault")
    public R<String> isDefault(@RequestBody Account account){
        LambdaQueryWrapper<Account> wrapper = new LambdaQueryWrapper<>();
        //默认地址修改为否
        wrapper.eq(Account::getIsDefault, 0);
        Account one = accountService.getOne(wrapper);
        one.setIsDefault(1);
        accountService.updateById(one);

        Account byId = accountService.getById(account.getId());
        byId.setIsDefault(0);
        accountService.updateById(byId);
        return R.success("", "修改成功");

    }


    /**
     * 修改银行卡
     * @param account
     * @return
     */
    @PutMapping("/update")
    public R<String> update(@RequestBody Account account){
        log.info("账户信息"+account);
        LambdaQueryWrapper<Account> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Account::getBankCardNumber,account.getBankCardNumber());
        int count = accountService.count(wrapper);
        if(count>=2){
            throw new CustomException("该银行卡已添加");
        }

        Account byId = accountService.getById(account.getId());
        //判断该银行卡是否已添加
        if(!account.getBankCardNumber().equals(byId.getBankCardNumber()) ){
            account.setTotalMenoy(0);
            account.setKyMenoy(0);
            account.setDjMenoy(0);
        }
        accountService.updateById(account);
        return R.success("","修改成功");
    }

    /**
     * 删除银行卡信息
     * @param id
     * @return
     */
    @DeleteMapping("/del")
    public R<String> del(Long id){
        //删除对应的交易明细记录
        LambdaQueryWrapper<AccountLog> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AccountLog::getBankId,id);
        accountLogService.remove(wrapper);

        accountService.removeById(id);
        return  R.success("","删除成功");
    }

    /**
     * 银行卡列表
     * @return
     */
    @GetMapping("/list")
    public R<List<Account>> list( Long id ){
        LambdaQueryWrapper<Account> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Account::getUserId,id);
        List<Account> list = accountService.list(wrapper);
        return  R.success(list,"");
    }
}
